package src.main.java.org.collection.List;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListTest1 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("蜘蛛精");
        list.add("孙悟空");
        list.add("唐僧");
        list.add("猪八戒");

        list.add(2,"沙僧");//插入到指定位置
        String remove = list.remove(2);//删除指定位置元素
        System.out.println(remove);

        System.out.println(list.get(0));

        String s = list.set(0, "牛魔王"); //修改
        System.out.println(list);
        System.out.println("===遍历方式===");
        //for循环
        for (int i = 0; i < list.size(); i++) {
            String s1 = list.get(i);
            System.out.println(s1);
        }
        //迭代器
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //增强for
        for (String c:
             list) {
            System.out.println(c);
        }
        //lambda
        list.forEach(s1 -> System.out.println(s1));


    }
}
